package taskTracker.ui.lwuit;

import taskTracker.Interval;
import taskTracker.Task;

import com.sun.lwuit.ComboBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.List;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.DefaultListCellRenderer;
import com.sun.lwuit.list.ListModel;

/**
 * A dialog that shows the current status of the program.
 * It shows whether the program is currently stopped or logging time.
 * When logging time it shows you where you are logging time project/task,
 * and for how long time has been logged.
 * You can also edit the comment, change the time out to show the this dialog again.
 * Finally, you can dismiss this window or start/stop time logging.
 * @author aseldawy
 *
 */
public class StatusAlert extends Dialog {
	
	/**A combo box that allows you to choose another project/task*/
	final ComboBox cmbCurrentTask = new ComboBox();
	
	/**A text area that allows you to change current notes.*/
	final TextArea txtComment = new TextArea();
	
	/**A combo box that allows you to choose how often this dialog appears*/
	final ComboBox cmbAlertAfter = new ComboBox(new Interval[] {
			new Interval(10*1000), Interval.minutes(2), Interval.minutes(5), Interval.minutes(10), Interval.minutes(15),
			Interval.minutes(30), Interval.hours(1), Interval.hours(2) });
	
	/**Hides this dialog without any changes.*/
	final Command cmdHide = new Command("Hide");
	
	/**Starts logging time. Only visible when current status is stopped.*/
	final Command cmdStart = new Command("Start");
	
	/**Stops logging time. Only visible when current status is logging.*/
	final Command cmdStop = new Command("Stop");
	
	/**
	 * Creates a new StatusAlert dialog
	 * @param alertAfterModel
	 */
	public StatusAlert() {
		super("TaskTracker");
		this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
		this.addComponent(cmbCurrentTask);
		cmbCurrentTask.setListCellRenderer(new DefaultListCellRenderer() {

			public Component getListCellRendererComponent(List list,
					Object obj, int arg2, boolean arg3) {
				Task task = (Task) obj;
				return super.getListCellRendererComponent(list, task.getName()+" / "+task.getParent().getName(), arg2, arg3);
			}
			
		});
		this.addComponent(txtComment);
		this.addComponent(cmbAlertAfter);
		this.addCommand(cmdHide);
		this.addCommand(cmdStart);
		this.addCommand(cmdStop);
	}
	
	/**
	 * Prepares dialog to show the given information.
	 * @param topTasks - Tasks to be shown to the user to select from. Must be shown in this order.
	 * @param notes - notes entered by the user.
	 * @param alertAfter - how often this dialog appears.
	 * @param logging - whether the application is currently logging time or not.
	 */
	public void prepare(Task[] topTasks, String notes, boolean logging, Task currentTask) {
		// TODO aseldawy enhancement: don't remove tasks if they're the same
		ListModel tasksModel = cmbCurrentTask.getModel();
		// remove all tasks
		while (tasksModel.getSize() > 0) {
			tasksModel.removeItem(0);
		}
		// add new ones
		for (int i=0;i<topTasks.length;i++) {
			tasksModel.addItem(topTasks[i]);
			if (topTasks[i] == currentTask)
				tasksModel.setSelectedIndex(i);
		}
		this.txtComment.setText(notes);
		if (logging) {
			this.removeCommand(cmdStart);
			this.addCommand(cmdStop);
		} else {
			this.removeCommand(cmdStop);
			this.addCommand(cmdStart);
		}
	}

	public Task getTask() {
		return (Task) cmbCurrentTask.getSelectedItem();
	}

	public String getComment() {
		return txtComment.getText();
	}

	public long getInterval() {
		return ((Interval)cmbAlertAfter.getSelectedItem()).period;
	}
}
